package org.nearby.note.utils;

import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;

public class common {
	public static String getUnique(){
		try{
			return md5(getMac() + getLocalIp() + getCPUSerial() + getHdSerialInfo() + "lijianwei");
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

	public static String getMac() {
		try {
			InetAddress address = InetAddress.getByName(getLocalIp());
			NetworkInterface nit = NetworkInterface.getByInetAddress(address);
			if (nit != null) {
				byte[] mac = nit.getHardwareAddress();
				if (mac != null) {
					return mac.toString();
				} else {
					System.out
							.println("Address doesn't exist or is not accessible.");
				}
			} else {
				System.out
						.println("Network Interface for the specified address is not found.");
			}
		} catch (UnknownHostException e) {
			e.printStackTrace();
		} catch (SocketException e) {
			e.printStackTrace();
		}

		return null;
	}

	public static String getLocalIp() {
		try {
			return InetAddress.getLocalHost().getHostAddress();
		} catch (UnknownHostException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String getCPUSerial() {
		String result = "";
		try {
			File file = File.createTempFile("tmp", ".vbs");
			file.deleteOnExit();
			FileWriter fw = new java.io.FileWriter(file);

			String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\. \\root\\cimv2\")\r\n"
					+ "Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\")  \r\n "
					+ "For Each objItem in colItems\r\n "
					+ "    Wscript.Echo objItem.ProcessorId  \r\n "
					+ "    exit for  \r\n" + "Next";
			fw.write(vbs);
			fw.close();
			Process p = Runtime.getRuntime().exec(
					"cscript //NoLogo " + file.getPath());
			BufferedReader input = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			String line;
			while ((line = input.readLine()) != null) {
				result += line;
			}
			input.close();
			file.delete();
		} catch (Exception e) {
			e.fillInStackTrace();
		}
		if (result.trim().length() < 1 || result == null) {
			result = "无CPU_ID被读取";
		}
		return result.trim();
	}

	public static String getHdSerialInfo() {

		String line = "";
		String HdSerial = "";// 定义变量 硬盘序列号

		try {
			Process proces = Runtime.getRuntime().exec("cmd /c dir c:");// 获取命令行参数
			BufferedReader buffreader = new BufferedReader(
					new InputStreamReader(proces.getInputStream()));

			while ((line = buffreader.readLine()) != null) {
				if (line.indexOf("卷的序列号是 ") != -1) { // 读取参数并获取硬盘序列号
					HdSerial = line.substring(line.indexOf("卷的序列号是 ")
							+ "卷的序列号是 ".length(), line.length());
					break;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return HdSerial;
	}
	public static  String md5(String s) throws UnsupportedEncodingException, NoSuchAlgorithmException{
		MessageDigest md5 = MessageDigest.getInstance("MD5");
		return md5.digest(s.getBytes("utf-8")).toString();
	}
	/**
	 * 根据窗口大小 返回窗口居中的坐标
	 * @param win_width  窗口宽度
	 * @param win_height 窗口高度
	 * @return 
	 */
	public static   HashMap<String,Integer> getWindowCenterSize(int win_width, int win_height){
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		HashMap<String, Integer> map = new HashMap<String, Integer>();
		map.put("width", (int)(screenSize.getWidth() - win_width)/2);
		map.put("height", (int)(screenSize.getHeight() - win_height)/2);
		return map;
	}
	/**
	 * 仿php time返回秒数
	 */
	public static String time(){
		String timeMill = String.valueOf(System.currentTimeMillis());
		return timeMill.substring(0, timeMill.length() - 3);
	}
}
